package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;

import com.icss.beans.JobInfoVO;
import com.icss.beans.User;
import com.icss.daos.JobInfoDao;
import com.icss.daos.UserDao;
import com.icss.utils.RecordSet;

/**
 * @author 韦锦良
 *　类的作用：就业信息servlet类
 * 最后修改时间：2010-3-13　下午03:56:35
 * 最后的改动内容：初始版本
 */
@SuppressWarnings("serial")
public class JobInfoServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		JobInfoDao dao = new JobInfoDao();
		String role = request.getParameter("role");
		HttpSession session = request.getSession();
		String disPatcherURL = null;

		//添加就业信息
		if ("add".equals(action)) {

			String uid = request.getParameter("uid");
			String workingCompany = request.getParameter("workingCompany");
			String enterCompanyTime = request.getParameter("enterCompanyTime");
			String leaveCompanyTime = request.getParameter("leaveCompanyTime");
			String visiable = request.getParameter("visiable");
			String salary = request.getParameter("salary");
			String sql = "";

			if ("至今".equals(leaveCompanyTime)) {

				sql = "insert into jobInfo(uid,workingCompany,enterCompanyTime,visiable,salary) values(" + uid + ","
						+ "'" + workingCompany + "','" + enterCompanyTime + "'," + visiable + "," + salary + ")";
			} else {

				sql = "insert into jobInfo(uid,workingCompany,enterCompanyTime,leaveCompanyTime,visiable,salary) values("
						+ uid
						+ ","
						+ "'"
						+ workingCompany
						+ "','"
						+ enterCompanyTime
						+ "','"
						+ leaveCompanyTime
						+ "',"
						+ visiable + "," + salary + ")";
			}

			boolean result = dao.execute(sql);
			PrintWriter out = response.getWriter();
			response.setContentType("text/html");

			if ("officeAdmin".equals(role)) {
				disPatcherURL = "jobInfo?action=admin_list";
			} else if ("officeUser".equals(role)) {
				disPatcherURL = "jobInfo?action=person_jobInfo";
			}

			if (result) {
				out.write("<script>alert('添加成功');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('添加失败');window.location.href='" + disPatcherURL + "';</script>");
			}

			out.close();

		}

		//删除就业信息
		if ("delete".equals(action)) {

			String jobInfoID = request.getParameter("jobInfoID");
			String sql = "delete from JobInfo where  jobInfoID=" + jobInfoID;
			boolean result = dao.execute(sql);
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();

			if ("officeAdmin".equals(role)) {
				disPatcherURL = "jobInfo?action=admin_list";
			} else {
				disPatcherURL = "jobInfo?action=person_jobInfo";
			}

			if (result) {
				out.write("<script>alert('删除成功');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('删除失败');window.location.href='" + disPatcherURL + "';</script>");
			}

			out.close();
		}
		
		//批量删除就业信息
		if ("batchingDelete".equals(action)) {

			String[] jobInfoIds = request.getParameterValues("jobInfoIds");
			StringBuffer sql = new StringBuffer("delete from JobInfo where  jobInfoID=");

			if (jobInfoIds.length == 1) {
				sql.append(jobInfoIds[0]);
			} else {

				for (int i = 0; i < jobInfoIds.length; i++) {
					if (i == jobInfoIds.length - 1) {
						sql.append(jobInfoIds[i]);
					} else {
						sql.append(jobInfoIds[i]).append(" or ").append("jobInfoID=");
					}
				}
			}
			boolean result = dao.execute(sql.toString());

			PrintWriter out = response.getWriter();

			if ("officeAdmin".equals(role)) {
				disPatcherURL = "jobInfo?action=admin_list";
			} else {
				disPatcherURL = "jobInfo?action=person_jobInfo";
			}

			if (result) {
				out.write("<script>alert('删除成功');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('删除失败');window.location.href='" + disPatcherURL + "';</script>");
			}

			out.close();

		}

		//获得所有的学生信息
		if ("getAllStudentInfos".equals(action)) {

			UserDao userDao = new UserDao();
			ArrayList<User> users = userDao.getAllUser();
			JSONArray usersJson = JSONArray.fromObject(users);
			PrintWriter out = response.getWriter();
			out.print(usersJson);
			out.close();
		}

		//修改就业信息
		if ("update".equals(action)) {

			String uid = request.getParameter("uid");
			String workingCompany = request.getParameter("workingCompany");
			String enterCompanyTime = request.getParameter("enterCompanyTime");
			String leaveCompanyTime = request.getParameter("leaveCompanyTime");
			String visiable = request.getParameter("visiable");
			String salary = request.getParameter("salary");
			String jobInfoID = request.getParameter("jobInfoID");

			String sql = "";

			if ("至今".equals(leaveCompanyTime)) {
				sql = "update jobInfo set uid=" + uid + ",workingCompany='" + workingCompany + "',enterCompanyTime='"
						+ enterCompanyTime + "',visiable=" + visiable + ",salary=" + salary + " where jobInfoID = "
						+ jobInfoID;
			} else {
				sql = "update jobInfo set uid=" + uid + ",workingCompany='" + workingCompany + "',enterCompanyTime='"
						+ enterCompanyTime + "',leaveCompanyTime='" + leaveCompanyTime + "',visiable=" + visiable
						+ ",salary=" + salary + " where jobInfoID = " + jobInfoID;

			}

			boolean result = dao.execute(sql);
			PrintWriter out = response.getWriter();
			response.setContentType("text/html");
			if ("officeAdmin".equals(role)) {
				disPatcherURL = "jobInfo?action=admin_list";
			} else {
				disPatcherURL = "jobInfo?action=person_jobInfo";
			}

			if (result) {
				out.write("<script>alert('修改成功');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('修改失败');window.location.href='" + disPatcherURL + "';</script>");
			}

			out.close();

		}

		//办公系统管理员身份请求就业信息列表
		if ("admin_list".equals(action)) {

			String strCurrentPage = request.getParameter("currentPage");
			String strPageSize = request.getParameter("pageSize");
			String type = request.getParameter("type");
			String typeArg = request.getParameter("typeArg");

			int currentPage = 1;
			int pageSize = 10;

			if (null != strCurrentPage) {
				currentPage = Integer.parseInt(strCurrentPage);
			}

			if (null != strPageSize) {
				pageSize = Integer.parseInt(strPageSize);
			}

			RecordSet rs = dao.getInfoForAdmin(pageSize, currentPage, type, typeArg);
			request.setAttribute("jobInfos", rs);
			request.getRequestDispatcher("/page/jiuye/jiuye_list.jsp").forward(request, response);
		}

		//办公系统用户请求自己的所有就业信息
		if ("person_jobInfo".equals(action)) {

			int uid = ((User) request.getSession(false).getAttribute("User")).getUid();
			ArrayList<JobInfoVO> jobInfos = dao.geJobInfoVOsByUid(uid);
			request.setAttribute("jobInfos", jobInfos);
			request.getRequestDispatcher("/page/jiuye/person_jiuye.jsp").forward(request, response);

		}
		
		//用户请求更新就业信息
		if ("updateRequest".equals(action)) {

			String jobInfoID = request.getParameter("jobInfoID");
			JobInfoVO jobInfo = dao.getJobInfoVOById(jobInfoID);
			request.setAttribute("jobInfo", jobInfo);
			request.setAttribute("role", role);
			request.getRequestDispatcher("/page/jiuye/updateJiuye.jsp").forward(request, response);

		}

		//设置就业信息的可见性
		if ("setPublic".equals(action)) {

			String jobInfoID = request.getParameter("jobInfoID");
			byte visiable = Byte.parseByte(request.getParameter("visiable"));
			String sql = "update JobInfo set visiable = " + visiable + " where JobInfoID=" + jobInfoID;
			boolean result = dao.execute(sql);
			int uid = ((User) session.getAttribute("User")).getUid();

			if ("officeAdmin".equals(role)) {
				disPatcherURL = "jobInfo?action=admin_list";
			} else {
				disPatcherURL = "jobInfo?action=person_jobInfo&uid=" + uid;
			}

			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			if (result) {
				out.write("<script>alert('设置成功');window.location.href='" + disPatcherURL + "';</script>");
			} else {
				out.write("<script>alert('设置失败');window.location.href='" + disPatcherURL + "';</script>");
			}
			out.flush();

		}

		//为前台获得就业信息列表
		if ("list_farward".equals(action)) {

			ArrayList<JobInfoVO> vos = dao.getJobInfoForFoward();
			request.setAttribute("list_all", vos);
			request.getRequestDispatcher("/page/jiuye/forwardJiuyeList.jsp").forward(request, response);

		}

		dao.close();
	}
}
